#!/bin/bash

# chmod +x convert_audio_to_ogg.sh
# 检查参数数量
if [ "$#" -lt 5 ]; then
    echo "Usage: $0 <video_file> <audio_file> <output_file> <start_time> <end_time> "
    exit 1
fi

# 获取输入和输出文件名

# 从参数中获取文件路径和时间
video_file="$1"
audio_file="$2"
output_file="$3"
start_time="$4"
end_time="$5"


# 使用 ffmpeg 合并视频和音频
ffmpeg -i "$video_file" -i "$audio_file" \
       -filter_complex "[1:a]atrim=start=$start_time:end=$end_time,volume=0.3[a1];[0:a][a1]amix=inputs=2:duration=first[a]" \
       -map 0:v -map "[a]" \
       -c:v copy -c:a aac -y "$output_file"

# 检查 ffmpeg 是否成功执行
if [ $? -ne 0 ]; then
    echo "Failed to merge video and audio. Please check your input files and parameters."
    exit 1
else
    echo "Video and audio merged successfully."
fi
